<?php
namespace App\Config;

class Response
{
    /**
     * 构造函数
     *
     * Response constructor.
     */
    public function __construct ()
    {
        #code...
    }

    /**
     * 输出响应结果
     *
     * @param $response
     */
    public function response ($response)
    {
        header('Content-Type:Application/json; Charset=utf-8');
        echo json_encode($response, JSON_UNESCAPED_UNICODE);
        exit();
    }

    /**
     * REST风格 成功响应
     *
     * @param $response 响应内容
     */
    public function restfulSuccess ($response)
    {
        header('Content-Type:Application/json; Charset=utf-8');
        echo json_encode([
            'code'    => 200,
            'message' => 'OK',
            'result'  => $response
        ], JSON_UNESCAPED_UNICODE);
        return;
    }

    /**
     * 命令行格式下成功响应
     *
     * @param $response 响应内容
     */
    public function cliModuleSuccess ($response)
    {
        var_dump([
            'code'    => 200,
            'message' => 'OK',
            'result'  => $response
        ]);
    }

    /**
     * REST风格失败响应
     *
     * @param int    $code     响应码
     * @param string $message  响应消息
     * @param        $response 响应内容
     */
    public function restfulFail ($code = 500, $message = 'Internet Server Error', $response)
    {
        header('Content-Type:Application/json; Charset=utf-8');
        echo json_encode([
            'code'    => $code,
            'message' => $message,
            'result'  => $response
        ], JSON_UNESCAPED_UNICODE);
        return;
    }

    /**
     * 魔法函数__get.
     *
     * @param string $name 属性名称
     *
     * @return mixed
     */
    public function __get ($name = '')
    {
        return $this->$name;
    }

    /**
     * 魔法函数__set.
     *
     * @param string $name  属性名称
     * @param mixed  $value 属性值
     *
     * @return mixed
     */
    public function __set ($name = '', $value = '')
    {
        $this->$name = $value;
    }
}